package org.jsola.contract.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.contract.example.ContractExample.E_PUBLISH_DATE_END;
import static org.jsola.contract.example.ContractExample.E_PUBLISH_DATE_START;
import static org.jsola.contract.example.ContractExample.N_PUBLISH_DATE_END;
import static org.jsola.contract.example.ContractExample.N_PUBLISH_DATE_START;
import static org.jsola.contract.example.PartySignExample.E_ACCOUNT_ID;
import static org.jsola.contract.example.PartySignExample.E_CONTRACT_ID;
import static org.jsola.contract.example.PartySignExample.E_SEAL_ID;
import static org.jsola.contract.example.PartySignExample.E_SEAL_POSITION_ID;
import static org.jsola.contract.example.PartySignExample.E_SIGN_DATE_END;
import static org.jsola.contract.example.PartySignExample.E_SIGN_DATE_START;
import static org.jsola.contract.example.PartySignExample.E_STATUS;
import static org.jsola.contract.example.PartySignExample.E_SUBJECT_ID;
import static org.jsola.contract.example.PartySignExample.N_ACCOUNT_ID;
import static org.jsola.contract.example.PartySignExample.N_CONTRACT_ID;
import static org.jsola.contract.example.PartySignExample.N_SEAL_ID;
import static org.jsola.contract.example.PartySignExample.N_SEAL_POSITION_ID;
import static org.jsola.contract.example.PartySignExample.N_SIGN_DATE_END;
import static org.jsola.contract.example.PartySignExample.N_SIGN_DATE_START;
import static org.jsola.contract.example.PartySignExample.N_STATUS;
import static org.jsola.contract.example.PartySignExample.N_SUBJECT_ID;

/**
 * 合同签署方签署记录
 *
 * @author JanusMix
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class PartySignQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_ACCOUNT_ID, example = E_ACCOUNT_ID)
    @Length(max = 40)
    private String accountId;

    @ApiParam(value = N_CONTRACT_ID, example = E_CONTRACT_ID)
    private Long contractId;

    @ApiParam(value = N_SEAL_ID, example = E_SEAL_ID)
    private Long sealId;

    @ApiParam(value = N_SEAL_POSITION_ID, example = E_SEAL_POSITION_ID)
    private Long sealPositionId;

    @ApiParam(value = N_SUBJECT_ID, example = E_SUBJECT_ID)
    @Length(max = 32)
    private String subjectId;

    /**
     * 签署时间查询范围
     */
    @ApiParam(value = N_SIGN_DATE_START, example = E_SIGN_DATE_START)
    private Date signDateStart;
    @ApiParam(value = N_SIGN_DATE_END, example = E_SIGN_DATE_END)
    private Date signDateEnd;


    @ApiParam(value = N_STATUS, example = E_STATUS)
    private Integer status;


    @ApiParam(value = org.jsola.contract.example.ContractExample.N_STATUS, example = org.jsola.contract.example.ContractExample.E_STATUS)
    private Integer contractStatus;

    @ApiParam(value = "小程序是否全部展示,true:全部展示,false:只展示需要展示的,合同关闭小程序展示的,不显示")
    private Boolean appAllShow;

    /**
     * 合同开始日期查询范围
     */
    @ApiParam(value = N_PUBLISH_DATE_START, example = E_PUBLISH_DATE_START)
    private Date contractPublishDateStart;
    @ApiParam(value = N_PUBLISH_DATE_END, example = E_PUBLISH_DATE_END)
    private Date contractPublishDateEnd;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;

    @ApiParam(value = "合同id", example = "1")
    private List<Long> contractIdList;

    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public PartySignQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

